var UrlParse = require('COMMON/js/util.url.parse.query.js');
var TargetUrlData = require("../common/zft.target.url");
var UrlAndLogo = require("../common/zft.url.icon.option");
var ParseTemplate = ZFT.Util.ParseTemplate;
var Ajax = ZFT.Util.Ajax;
var STip = ZFT.Util.STip;
var Template = {
    menu: ParseTemplate(require("./tpl/menu.xtpl"))
}
var Main = ZFT.Util.Class({
    container: $("body"),
    targetType: {
        "2": "0", "3": "1", "4": "1"
    },
    init: function () {
        var _this = this;
        _this.loadMenu();
        $("#dropMenu .J_tabRefresh").on("click", function () {
            var href = $(".active.J_menuTab").attr("data-id");
            var $iframe = $(".J_iframe:visible");
            var $iframe_src = $iframe.attr("src");
            if (href == $iframe.attr("src") || !href) $iframe.attr("src", $iframe_src).ready();
        });

        $("#dropMenu").on("mouseleave", function (e) {
            $("#rollRightBtn").removeClass("open");
        });
        _this.checkTar();
    },
    //根据url参数获取响应的地址
    checkTar: function () {
        var _this = this,
            url = window.location.href,
            urlParam = UrlParse(url);
        var tar = urlParam.tar;
        var $_iframe = $(".J_iframe:visible");
        var dType = ZFT.Util.getDtype();
        if (_this.targetType.hasOwnProperty(dType) && tar == _this.targetType[dType]) {
            $_iframe.attr("src", TargetUrlData[tar]).ready();
        }
    },

    loadMenu: function () {
        var _this = this;
        Ajax("/index/index/getPower", {
            type: "POST",
            dataType: "json",
            success: function (res) {
                if (res.code == 200) {
                    var data = res.data,
                        menu, newData,
                        menu = data.menu,
                        defaultPage = data.default,
                        pages = _this.transArr(menu);
                    newData = _this.addUrlAndIcon(menu);
                    //$("#menu").attr("src", '//static.louxiaomei.com/assets/build/js/widget/metisMenu/jquery.metisMenu.js');
                    menu = Template.menu({ data: newData });
                    $("#side-menu").append(menu).metisMenu();

                    setTimeout(function () {
                        // $("#side-menu").metisMenu();
                        //$("#plus").attr("src", '//static.louxiaomei.com/assets/build/js/widget/hplus.min.js');
                        $.getScript("//static.louxiaomei.com/assets/build/js/widget/contabs.min.js")
                    }, 200);
                    //console.log($.inArray(defaultPage,pages)!==-1,defaultPage,pages);
                    $("#iframe0").attr("src", "/index/index/" + (($.inArray(defaultPage, pages) !== -1) ? UrlAndLogo[defaultPage].url : newData[0].child[0].url)).ready();
                } else if (res.code == 201) {
                    return alert(res.msg)
                }
            }
        })
    },
    transArr: function (data) {
        var arr = [];
        for (var i = 0, len = data.length; i < len; i++) {
            if (data[i].type == 3) {
                arr.push(data[i].id);
            }
        }
        return arr;
    },
    addUrlAndIcon: function (data) {
        var typea = [],
            typeb = [],
            childa = [];

        for (var i = 0, len = data.length; i < len; i++) {
            var dataI = data[i];
            if (dataI.type == 1) {
                typea.push(dataI);
            }
            if (dataI.type == 2) {
                dataI["url"] = UrlAndLogo[dataI.id].url;
                dataI["icon"] = UrlAndLogo[dataI.id].icon;
                typeb.push(dataI);
            }
        }
        for (var a = 0, leng = typea.length; a < leng; a++) {
            for (var b = 0, len = typeb.length; b < len; b++) {
                if (typeb[b]["pId"] == typea[a]["id"]) {
                    childa.push(typeb[b])
                }

            }
            typea[a]["child"] = childa;
            childa = [];
        }
        return typea;

    }

})
    (function () {
        new Main();
    })